package game.ouroboros.screens;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import framework.Engine;
import framework.Input.TouchEvent;
import framework.graphics.opengl.Camera2D;
import framework.graphics.opengl.GLScreen;
import framework.graphics.opengl.SpriteBatcher;
import framework.maths.Circle;
import framework.maths.CollisionTester;
import framework.maths.Vector2;
import game.ouroboros.Assets;

/**
 * TODO Função para reconhecer toque área retangular e circular TODO Transitar
 * para a tela de jogo
 * 
 * @author lucas
 * 
 */
public class MenuGameScreen extends GLScreen {
	Camera2D cam;
	SpriteBatcher batcher;
	Circle buttonBounds;
	Vector2 touchPoint;

	public MenuGameScreen(Engine engine) {
		super(engine);
		Log.v("<Chamada>","MenuGameScreen()");
		buttonBounds = new Circle(166.5f, 355.5f, 55.5f);
		
		cam = new Camera2D(glGraphics, 320, 480); // resolução 320x480 TODO
													// Carregar de um arquivo
		batcher = new SpriteBatcher(glGraphics, 100);
		touchPoint = new Vector2();
		Log.v("<FIM>","MenuGameScreen()");
	}

	@Override
	public void update(float deltaTime) {
		Log.v("<Chamada>","MenuGameScreen.update()");
		List<TouchEvent> touchEvents = engine.getInput().getTouchEvents();
		int len = touchEvents.size();
		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				touchPoint.set(event.x, event.y);
				cam.touchToWorld(touchPoint); // converte as coordenas do touch
												// para as do mundo
				if (CollisionTester.pointInCircle(buttonBounds, touchPoint)) {
					engine.setScreen(new RunningGameScreen(engine));
				}
			}
		}

		Log.v("<FIM>","MenuGameScreen.update()");
	}

	@Override
	public void render(float deltaTime) {

		Log.v("<Chamada>","MenuGameScreen.render()");
		GL10 gl = glGraphics.getGL();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		cam.setViewportAndMatrices();
		gl.glEnable(GL10.GL_TEXTURE_2D);
		batcher.beginBatch(Assets.menuBackGround);
		batcher.drawExactSprite(0, 0, 320, 480, Assets.menuBackGroundRegion);
		batcher.endBatch();

		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

		batcher.beginBatch(Assets.botoes);
		batcher.drawExactSprite(111, 300, 111,111, Assets.botaoJogar);
		batcher.endBatch();

		gl.glDisable(GL10.GL_BLEND);

		Log.v("<FIM>","MenuGameScreen.render()");
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
